﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameMgr : MonoBehaviour {
    private static GameMgr _instance;

    public GameObject JumperPrefab;

    public static GameMgr Instance()
    {
        return GameMgr._instance;
    }

	// Use this for initialization
	void Start () {
        GameMgr._instance = this;
        mainObjectID = 1;
        entities = new Dictionary<ulong, Entity>();

        {
            Entity entity = new Entity();
            entity.identify.objectID = 1;
            entities[entity.identify.objectID] = entity;
            entity.render.gameObject = GameObject.Instantiate(JumperPrefab);
        }

        {
            Entity entity = new Entity();
            entity.identify.objectID = 2;
            entity.movement.lineSpeed = 0;
            entity.position.pos.x = 2000;
            entity.position.pos.y = 2000;
            entities[entity.identify.objectID] = entity;
            entity.render.gameObject = GameObject.Instantiate(JumperPrefab);
        }

        InputSystem.Init();
    }
	
	// Update is called once per frame
	void Update () {
        //GameObject.Find("Canvas/Text").GetComponent<Text>().text = Input.touchCount.ToString();
    }

    private void FixedUpdate()
    {
        InputSystem.Action();
        CollideSystem.Action();

        foreach (KeyValuePair<ulong, Entity> e in entities)
        {
            CommandSystem.Action(e.Value);

            RotationSystem.Action(e.Value);
            MoveSystem.Action(e.Value);
            RenderSystem.Action(e.Value);
        }
    }

    private Dictionary<ulong, Entity> entities;
    private ulong mainObjectID;
    public Entity MainEntity(){
        Entity e;
        if (entities.TryGetValue(mainObjectID, out e)){
            return e;
        }
        return null;
    }
    public delegate void EntityFunction(Entity entity);
    public void ForeachEntity(EntityFunction foo){
        foreach (KeyValuePair<ulong, Entity> e in entities)
        {
            foo(e.Value);
        }
    }
}
